package com.csi.jf.crucible.common.controller;

import javax.servlet.http.HttpSession;

import com.chinasofti.ro.bizframework.core.mvc.Controller;
import com.chinasofti.ro.bizframework.core.mvc.View;
import com.chinasofti.ro.bizframework.modules.json.JSONArray;
import com.csi.jf.crucible.common.config.JfConstants;
import com.csi.jf.crucible.common.model.JfTUser;
import com.csi.jf.crucible.common.service.LoginService;
import com.csi.jf.crucible.common.util.LoginUtils;

/**
 * <p>controller</p>
 * 
 * @author BizFoundation
 * @version 1.0
 * @since 1.0
 */
public class LoginController extends Controller {
	
	/**
	 * <p>显示登录界面</p>
	 * 
	 */
    public void show() {
        View view = new View(SUCCESS);
        this.render(view);
    }
	/**
	 * <p>显示用户登录界面</p>
	 */
	public void home() {
	    View view = null;
	    HttpSession session = request().getSession(false);
	    if (session != null) {
	        JfTUser u = (JfTUser)session.getAttribute(JfConstants.LOGIN_USER);
            if(null!=u){
                //跳转到用户首页
                String role = u.getRole();
                if(role==null || "".equals(role.trim())){
                    //跳转到错误页面或登录页面
                    view = new View(FAILED);
                }else if(JfConstants.ROLE_PM.equals(role)){
                    view = new View("pm");
                }else if(JfConstants.ROLE_DEV.equals(role)){
                    view = new View("dev");
                }else if(JfConstants.ROLE_OPT.equals(role)){
                    view = new View("opt");
                }else if(JfConstants.ROLE_PRO.equals(role)){
                    view = new View("pro");
                }
            }else{
                //返回登录页面
                view = new View(FAILED);
                //清除掉已登录用户信息;
                session.removeAttribute(JfConstants.LOGIN_USER);
            }
        }else{
            //返回登录页面
            view = new View(FAILED);
        }
	    
	    this.render(view);
	}
	
	/**
	 * <p>判断登录</p>
	 * 
	 */
	public void login(String username,String password) {
		Boolean flag = false;
		JSONArray json = new JSONArray();
		//检查用户身份合法性
		JfTUser loginUser = loginService.ifLogin(username, password);
		if(loginUser != null){
			flag = true;
		}
        if (flag) {
            // 缓存登录用户信息
            HttpSession session = request().getSession();
            //JfTUser user = new JfTUser();
            //user.setJfid(loginUser.getJfid());
            //user.setUserName(username);
            //user.setUserPassword(userpassword);
            session.setAttribute(JfConstants.LOGIN_USER, loginUser);
            json.add(flag);
        }else{
			json.add(flag);
		}
		this.renderJSON(json.toString());
	}
	/**
	 * <p>退出登录</p>
	 * 
	 */
	public void logout(String username,String userpassword) {
		//消除登录用户信息
	    HttpSession session = request().getSession(false);
	    if(session!=null){
	        session.removeAttribute(JfConstants.LOGIN_USER);
	    }
	    //返回登录页面
        this.redirect("/login.jsp");
	}
	
	public void changeCurrentProjectID(Long projectId,String projectName){
	    LoginUtils.changeCurrentProjectID(projectId,projectName);
	}

	
    /**
     * <p>注入 codePackagesService</p>
     */
    private LoginService loginService;

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
}